package com.dr.web.controllers;

import com.dr.common.ution.ImgBase64Util;
import com.dr.common.ution.UploadImages;
import com.dr.exception.ServiceExcep;
import com.dr.pojo.WeChatLogin;
import com.dr.service.WeChatLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tugou on 2017/5/23.
 */
@Controller
@RequestMapping("appInerface")
public class AppInterfaceController {
    @Autowired
    WeChatLoginService weChatLoginService;

    @RequestMapping(value = "getCode",method= RequestMethod.GET)
    @ResponseBody
    public Map signIn(){
        Map<String,String> result = new HashMap<String, String>();
        try {
            WeChatLogin weChatLogin = weChatLoginService.createWeChatLoginInfo();
            result.put("msg","成功");
            result.put("code","00000");
            result.put("qrcode",weChatLogin.getQrcode());
        } catch (ServiceExcep excep) {
            result.put("msg",excep.getMsg());
            result.put("code", String.valueOf(excep.getCode()));
        }
        return  result;
    }

    @RequestMapping(value = "wechatsca",method= RequestMethod.GET)
    @ResponseBody
    public Map wechatsca(String qrcode){
        Map<String,String> result = new HashMap<String, String>();
        try {
            weChatLoginService.updateWeChatLogin(qrcode);
            result.put("msg","成功");
            result.put("code","00000");
        } catch (ServiceExcep excep) {
            result.put("msg",excep.getMsg());
            result.put("code", String.valueOf(excep.getCode()));
        }
        return  result;
    }

    @RequestMapping(value = "isLogin",method= RequestMethod.GET)
    @ResponseBody
    public Map isLogin(String qrcode){
        Map<String,String> result = new HashMap<String, String>();
        try {
            WeChatLogin weChatLogin = weChatLoginService.getWeChatLoginByCode(qrcode);
            if(weChatLogin.getState() == 1){
                result.put("msg","成功");
                result.put("code","00000");
            }
            else{
                result.put("msg","失败");
                result.put("code","10001");
            }
        } catch (ServiceExcep excep) {
            result.put("msg",excep.getMsg());
            result.put("code", String.valueOf(excep.getCode()));
        }
        return result;
    }

    @RequestMapping(value = "imgUpLoad",method= RequestMethod.POST)
    @ResponseBody
    public Map imgUpLoad(String filePath, String fileName, @RequestParam("image") MultipartFile image, HttpServletRequest request){
        Map<String,String> result = new HashMap<String, String>();
        try {
            UploadImages.saveFile(image,request,filePath,fileName);
            result.put("msg","成功");
            result.put("code","00000");
        }catch (Exception ex){
            result.put("msg",ex.getMessage());
            result.put("code","10002");
        }
        return result;
    }



}
